<template>
  <view>
    <view class="platform">
      <view class="item" @click="nowShare([1,2])">
        <button class="btn" hover-class="none">
          <image src="../../static/wx-icon.png" class="img"></image>
        </button>
      </view>
      <view class="item">
        <button class="btn" hover-class="none" @click="nowShare([3,4])">
          <image src="../../static/qq-icon.png" class="img"></image>
        </button>
      </view>
      <view class="item" v-if="false">
        <button class="btn" hover-class="none">
          <image src="../../static/weibo-icon.png" class="img"></image>
        </button>
      </view>
    </view>
    <share ref="onShare" :show='showLeng' @click="getShare"></share>
  </view>
</template>

<script setup>
import logoImage from '@/static/logo.png';
import { ref } from 'vue';
import { setUrl, toast, checkServe } from '@/common';

const emit = defineEmits(['confirm'])
const props = defineProps({
  url: {
    type: String,
    default: ''
  },
  title: {
    type: String,
    default: ''
  },
  detail: {
    type: Object,
    default: () => {}
  },
  pic: {
    type: String,
    default: ''
  }
})
const onShare = ref(null);
let showLeng = ref([]);

function nowShare(row){
  showLeng.value = row;
  onShare.value.open();
}
// 分享
function getShare(row){
  if(row.id == 5) toast('开发中...');
  let title = props.title;
  let summary = props.detail.content;
  let href = `${uni.$uv.http.config.webUrl}${props.url}?id=${props.detail.id}`;
  console.log(href);
  let imageUrl = props.pic ? setUrl(props.pic.split(',')[0]) : logoImage;
  
  if(row.id == 6) {
    uni.setClipboardData({
      data: href,
      success: function () {
        toast('复制成功');
      }
    });
    onShare.value.close();
    return;
  }
  function goShare(){
    let scene = {
      1: 'WXSceneSession',
      2: 'WXSceneTimeline'
    }
    uni.share({
      provider: row.share,
      scene: scene[row.id],
      type: 0,
      href,
      title,
      summary,
      imageUrl,
      success(res) {
        console.log('分享成功');
        emit('confirm');
      },
      fail(err) {
        console.log(JSON.stringify(err));
        console.log('分享失败');
      },
      complete() {
        console.log('初始化');
      }
    })
  }
  onShare.value.close();
  checkServe('share', row.share, goShare);
}
</script>

<style lang="scss" scoped>
  .platform{
    display: flex;
    align-items: center;
    justify-content: space-around;
    width: 100%;
    padding: 0 80rpx;
    .item{
      flex: 1;
      .btn{
        width: 80rpx;
        height: 80rpx;
        line-height: 80rpx;
        margin: 0 auto;
        background-image: inherit;
      }
    }
  }
</style>